package com.kid.keep.pattern;

import cn.hutool.core.lang.Assert;
import org.nfunk.jep.JEP;

/**
 * @Description 身体形态 计算方式
 * @Author JOHN
 * @Create 2020-01-03
 * @Since 1.0.0
 */
public class BodyShopeExpression {

    private static final String WEIGHT = "weight";
    private static final String HEIGHT = "height";
    private static final Integer MULTIPLY_NUMBER = 100;

    /**
     * 计算方式：体重指数（BMI）=体重（kg）÷身高^2（m）  因单位都乘以了100
     */
    private static final String FORMULA = "(" + WEIGHT + "*" + MULTIPLY_NUMBER + ")/" + "(" + HEIGHT + "^2)*"+MULTIPLY_NUMBER;


    public static Integer getBodyShopeResult(Integer weight, Integer height) throws Exception {
        Assert.notNull(weight, "weight is null");
        Assert.notNull(height, "height is null");
        JEP jep = new JEP();
        jep.addVariable(WEIGHT, weight);
        jep.addVariable(HEIGHT, height);
        jep.parseExpression(FORMULA);
        Double accrual = jep.getValue();
        return accrual.intValue();
    }

    public static void main(String[] args) throws Exception {
        System.out.println(getBodyShopeResult(1, 2));
    }
}
